using System.Linq;
using System.Xml.Linq;
using TheDFFL.Entities;

namespace TheDFFL.Services
{
    public static class XmlExtensions
    {
        private static readonly XNamespace yahoons = "http://fantasysports.yahooapis.com/fantasy/v2/base.rng";
        public static XNamespace YahooNS { get { return yahoons; } }

        public static Team ToTeam(this XElement element)
        {
            return new Team
            {
                TeamID = (int)element.Element(YahooNS + "team_id"),
                TeamKey = (string)element.Element(YahooNS + "team_key"),
                Name = (string)element.Element(YahooNS + "name"),
                TeamURL = (string)element.Element(YahooNS + "url"),
                LogoURL = (string)element.Descendants(YahooNS + "team_logo").First().Element(YahooNS + "url"),

                Wins = (int)element.Descendants(YahooNS + "outcome_totals").First().Element(YahooNS + "wins"),
                Losses = (int)element.Descendants(YahooNS + "outcome_totals").First().Element(YahooNS + "losses"),
                Ties = (int)element.Descendants(YahooNS + "outcome_totals").First().Element(YahooNS + "ties")
                /*
                * ,
                PointsScored = (decimal)element.Descendants(Yahoons + "team_points").First().Element(Yahoons + "total")
                */
            };
        }
    }
}